import path from 'path';
import { defineConfig, ConfigEnv, UserConfigExport } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { viteMockServe } from 'vite-plugin-mock';

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfigExport => {
    console.log('vite env', command, mode);
    return defineConfig({
        base: '/',
        server: {
            port: 3002,
            proxy: {
                '/api': {
                    target: 'http://localhost:3001/',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        },
        build: {
            sourcemap: true
            // manifest: true
            // rollupOptions: {
            //     output: {
            //         manualChunks(id) {
            //             if (id.includes('node_modules/element-plus')) return 'element-plus'
            //         }
            //     }
            // },
            // chunkSizeWarningLimit: 600
        },
        optimizeDeps: {
            // include: ['axios', 'nprogress', 'mockjs']
        },
        plugins: [
            vue(),
            vueJsx(),
            viteMockServe({
                supportTs: false,
                mockPath: 'mock',
                localEnabled: command === 'serve'
            })
        ],
        resolve: {
            alias: [
                { find: '/@', replacement: path.resolve(__dirname, 'src') },
                {
                    find: 'plugins',
                    replacement: path.resolve(__dirname, 'src/plugins')
                },
                {
                    find: 'styles',
                    replacement: path.resolve(__dirname, 'src/styles')
                },
                {
                    find: 'views',
                    replacement: path.resolve(__dirname, 'src/views')
                }
            ]
        }
    });
};
